package system

import (
	"atools/models"
	"fmt"
	"os/exec"
	"runtime"
)

type SystemInfo interface {
	GetInstalledApps(app *App) []models.InstalledApp
}

func (app *App) GetInstalledApps() ([]models.InstalledApp, error) {
	var apps []models.InstalledApp

	switch runtime.GOOS {
	case "darwin":
		apps = MacosInfo{}.GetInstalledApps(app)
	case "windows":
		apps = WindowsSystemInfo{}.GetInstalledApps(app)
	case "linux":
		apps = LinuxInfo{}.GetInstalledApps(app)
	default:
		apps = []models.InstalledApp{{Name: "Unsupported OS", Path: runtime.GOOS}}
	}

	return apps, nil
}

func (app *App) LaunchApp(path string) error {
	var cmd *exec.Cmd

	switch runtime.GOOS {
	case "darwin": // macOS
		cmd = exec.Command("open", path)
	case "windows": // Windows
		cmd = exec.Command("cmd", "/c", "start", "", path)
	case "linux": // Linux
		cmd = exec.Command("xdg-open", path)
	default:
		return fmt.Errorf("不支持的操作系统: %s", runtime.GOOS)
	}

	return cmd.Start()
}
